home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
6_11.lha
/
6_11
/
6_11_out.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
2KB
|
71 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
*
Output a type arbint to the given stream.
/
include <arbint.h>
include <debug.h> // DELETE
stream& operator<< (ostream& out, const arbint& j)
f (debug&2) outputarb(cerr, "\n\nout:\nj=", j.p->value, j.p->length); // DELETE
arbint val = +j;
f (debug&2) outputarb(cerr, "val=", val.p->value, val.p->length); // DELETEd // reverse the sign of negative numbers
if (j.isneg())
{
val = -j;
out << "-";
f (debug&2) outputarb(cerr, "val=", val.p->value, val.p->length); // DELETE
}
// output can be stored in 6*val.length hyper-decimal
// digits, each digit holding a value 0..9999.
const ARB_type dec_base = 10000;
f (debug&2) cerr << "val.p->length=" << val.p->length << ", ARB_base=" << ARB_base << ", dec_base=" << dec_base << "\n"; // DELETEd ARB_type *digs =
new ARB_type[val.p->length * ARB_base / dec_base];
ARB_type *svdigs = digs;
ARB_type *val_val = val.p->value;
int val_len = val.p->length;
// store "digits" in reverse order
for (;;)
{
// *digs++ = val % 10000
// val /= 10000;
ARB_Ltype prevu = 0;
for (int r = 0; r < val_len; r++)
{d ARB_Ltype tmp = val_val[r] +
prevu * ARB_base;
ARB_type tmpq = tmp / dec_base;
val_val[r] = tmpq; // % ARB_base
prevu = tmp - dec_base * tmpq;
}
*digs++ = prevu;d
bug&2) cerr << "\tdigit stored: " << prevu << "\n"; // DELETE
// if (val == 0)
// break;d
bug&2) outputarb(cerr, "val (after div)=", val_val, val_len); // DELETEd int allzero = 1;
for (r = 0; r < val_len; r++)
if (val_val[r] != 0)
{
allzero = 0;
break;
}
if (allzero)
break;d }
// Output digits in forward order.
// All but first digit must be 4 decimal
// digits in lengths, including leading zeros.
out << *--digs;
while (digs-- > svdigs)
out << form("%4.4u", *digs);
delete svdigs;
return out;